package ShopCar;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Goods[] shopcar = new Goods[100];
        String choice;
        while(true){
            System.out.println("编号\t\t商品名\t\t\t价格\t\t\t数量");
            for(int i=0;i<shopcar.length;i++)
            {
                if(shopcar[i]!=null)
                {
                    System.out.println(shopcar[i].id+"\t\t\t"+shopcar[i].name+"\t\t\t"+shopcar[i].price+"\t\t\t"+shopcar[i].num);
                }

            }
            menu();
            choice = scanner.next();
            switch (choice){
                case "add":
                {
                    add(shopcar,scanner);
                    break;
                }
                case "delete":
                {
                    delete(shopcar,scanner);
                    break;
                }
                case "change":
                {
                    change(shopcar,scanner);
                    break;
                }
                case "pay":
                {
                    double sum = end(shopcar);
                    System.out.println("总金额为:"+sum);
                    return;
                }
                default:
                {
                    System.out.println("选择错误");
                }
            }
        }
    }
    public static void menu(){
        System.out.println("--------------------------------------");
        System.out.println("添加物品:add");
        System.out.println("删除物品:delete");
        System.out.println("修改物品数量:change");
        System.out.println("结账:pay");
        System.out.println("--------------------------------------");
        System.out.println("请选择你的操作:");
    }
    public static void add(Goods[] shopcar,Scanner scanner){
        for(int i=0;i<shopcar.length;i++){
            if(shopcar[i]==null){
                Goods goods = new Goods();
                System.out.println("请输入商品编号");
                goods.id = scanner.next();
                System.out.println("请输入商品名称:");
                goods.name = scanner.next();
                System.out.println("请输入商品价格:");
                goods.price = scanner.nextDouble();
                System.out.println("请输入购买数量");
                goods.num = scanner.nextInt();
                shopcar[i] = goods;
                System.out.println(shopcar[i].num+"件"+shopcar[i].name+"已添加到购物车");
                break;
            }
        }
    }
    public static void delete(Goods[] shopcar,Scanner scanner){
        System.out.println("--------------------------------------");
        System.out.println("1.通过编号删除");
        System.out.println("2.通过商品名删除");
        System.out.println("--------------------------------------");
        System.out.println("请选择");
        int choice = scanner.nextInt();
        switch (choice){
            case 1:{
                int flag = 0;
                System.out.println("请输入编号:");
                String tmp = scanner.next();
                for(int i=0;i<shopcar.length;i++){
                    if(shopcar[i]==null)
                    {
                        continue;
                    }
                    if(shopcar[i].id.equals(tmp)){
                        shopcar[i]=null;
                        flag = 1;
                        break;
                    }
                }
                if(flag==0){
                    System.out.println("购物车中无此物品");
                }
                break;
            }
            case 2:{
                int flag = 0;
                System.out.println("请输入商品名:");
                String tmp = scanner.next();
                for(int i=0;i<shopcar.length;i++){
                    if(shopcar[i]==null)
                    {
                        continue;
                    }
                    if(shopcar[i].name.equals(tmp)){
                        shopcar[i]=null;
                        flag = 1;
                        break;
                    }
                }
                if(flag==0){
                    System.out.println("购物车中无此物品");
                }
                break;
            }
        }
    }
    public static void change(Goods[] shopcar,Scanner scanner){
        System.out.println("--------------------------------------");
        System.out.println("1.通过编号");
        System.out.println("2.通过商品名");
        System.out.println("--------------------------------------");
        System.out.println("请选择");
        int choice = scanner.nextInt();
        switch (choice){
            case 1:{
                int flag = 0;
                System.out.println("请输入编号:");
                String tmp = scanner.next();
                for(int i=0;i<shopcar.length;i++){
                    if(shopcar[i]==null)
                    {
                        continue;
                    }
                    if(shopcar[i].id.equals(tmp)){
                        System.out.println("输入要修改的数量");
                        shopcar[i].num = scanner.nextInt();
                        flag = 1;
                        break;
                    }
                }
                if(flag==0){
                    System.out.println("购物车中无此物品");
                }
                break;
            }
            case 2:{
                int flag = 0;
                System.out.println("请输入商品名:");
                String tmp = scanner.nextLine();
                for(int i=0;i<shopcar.length;i++){
                    if(shopcar[i]==null)
                    {
                        continue;
                    }
                    if(shopcar[i].name.equals(tmp)){
                        System.out.println("输入要修改的数量");
                        shopcar[i].num = scanner.nextInt();
                        flag = 1;
                        break;
                    }
                }
                if(flag==0){
                    System.out.println("购物车中无此物品");
                }
                break;
            }
        }
    }
    public static double end(Goods[] shopcar){
        double sum = 0;
        for(int i=0;i<shopcar.length;i++){
            if(shopcar[i]!=null){
                sum += (shopcar[i].price)*(shopcar[i].num);
            }
        }
        return sum;
    }
}
